// pages/my/my.js
import {
  httpRequest
} from "../../utils/request/http"

Page({
  data: {
    avatarUrl: "",
    userInfo: {},
    granted: {}, // 余额信息
    show: false,
  },
  onLoad: function () {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })

    if (!wx.getStorageSync('userInfo')) {
      this.setData({
        show: true
      })
      wx.showToast({
        title: '您还未登录',
        icon: 'none'
      })
      setTimeout(() => {
        this.setData({
          show: false
        })
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }, 2000);
    }
  },

  onChooseAvatar(e) {
    this.setData({
      avatarUrl: e.detail.avatarUrl
    })
  },
  /**余额获取 */
  CreditGrants() {

    httpRequest("/api/credit_grants", "post").then(res => {
      this.setData({
        granted: res
      })
    }).catch(err => {
      console.log(err)
    })
  },

  // 跳转
  onToLogin() {

    wx.navigateTo({
      url: "/pages/login/login"
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.CreditGrants()
  }
})